#
# @lc app=leetcode.cn id=13 lang=python3
#
# [13] 罗马数字转整数
#

# @lc code=start
class Solution:
    def romanToInt(self, s: str) -> int:
        num = 0
        for i in range(len(s)):
            if s[i] == 'I':
                num += 1
            elif s[i] == 'V':
                num += 5
                if i > 0 and s[i - 1] == 'I':
                    num -= 2
            elif s[i] == 'X':
                num += 10
                if i > 0 and s[i - 1] == 'I':
                    num -= 2
            elif s[i] == 'L':
                num += 50
                if i > 0 and s[i - 1] == 'X':
                    num -= 20
            elif s[i] == 'C':
                num += 100
                if i > 0 and s[i - 1] == 'X':
                    num -= 20
            elif s[i] == 'D':
                num += 500
                if i > 0 and s[i - 1] == 'C':
                    num -= 200
            elif s[i] == 'M':
                num += 1000
                if i > 0 and s[i - 1] == 'C':
                    num -= 200
        return num
                
# @lc code=end

